package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("admin/product")
public class SkuManageController {

    @Autowired
    private ManageService manageService;

    ///admin/product/saveSkuInfo
    //保存skuInfo
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        manageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    ////admin/product/list/{page}/{limit}
    //skuInfo列表分页
    @GetMapping("list/{page}/{limit}")
    public Result getSkuList(@PathVariable Long page,
                             @PathVariable Long limit,
                             SkuInfo skuInfo){
        Page<SkuInfo> skuInfoPage = new Page<>(page,limit);
        IPage<SkuInfo> iPage = manageService.getSkuList(skuInfoPage,skuInfo);
        return Result.ok(iPage);
    }

    ///admin/product/onSale/{skuId}
    //商品上架
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        manageService.onSale(skuId);
        return Result.ok();
    }


    ///admin/product/cancelSale/{skuId}
    //商品下架
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        manageService.cancelSale(skuId);
        return Result.ok();
    }

    //admin/product/getSkuInfo/20
    //数据回显
    @GetMapping("getSkuInfo/{skuId}")
    public Result getSkuInfo(@PathVariable Long skuId){
       return Result.ok(manageService.findSkuInfo(skuId));
    }

    //更新数据
    @PostMapping("updateSkuInfo")
    public Result updataSkuInfo(@RequestBody SkuInfo skuInfo){
        manageService.updateSkuInfo(skuInfo);
        return Result.ok();
    }


}
